﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product.RateFactorConverter
{
    public class InsurancePeriodConverter:AbstractRateFactorConverter
    {
        private static readonly string[] alias = new string[] { "保障期限", "保障年限", "保险期限", "保险年限"};

        private static readonly string onceTemplate = @"终生|终身|一生";

        private static readonly string periodTemplate = @"(\d{1,3})[年月]期?";

        private static readonly string ageTemplate = @"[到至]?(\d{1,3})周?岁";

        private static readonly string equalPayPeriodTemplate = "等于付款期";

        private static readonly string equalBasicTemplate = "等于主险";

        private static readonly string equalBasicPayPeriodTemplate = "等于主险付款期";

        public override string[] Alias
        {
            get { return alias; }
        }

        public static IInsurancePeriod CreateProperty(string item)
        { 
            IInsurancePeriod pp = new InsurancePeriod();

            Match m = Regex.Match(item, onceTemplate);
            if (m.Success)
            {
                
                pp.Style = InsurancePeriodStyle.Lifetime;
                pp.Value = 999;
                pp.Name = item;

                return pp;

            }

            m = Regex.Match(item, periodTemplate);

            if (m.Success)
            {

                pp.Style = InsurancePeriodStyle.Period;
                pp.Value = int.Parse(m.Groups[1].Value);
                pp.Name = item;

                return pp;

            }
            m = Regex.Match(item, ageTemplate);

            if (m.Success)
            {

                pp.Style = InsurancePeriodStyle.Age;
                pp.Value = int.Parse(m.Groups[1].Value);
                pp.Name = item;

                return pp;

            }

            if (item == equalPayPeriodTemplate)
            {
                pp.Style = InsurancePeriodStyle.EqualPayPeriod;
                pp.Value = 0;
                pp.Name = item;

                return pp;
            }

            if (item == equalBasicTemplate)
            {
                pp.Style = InsurancePeriodStyle.EqualBasic;
                pp.Value = 0;
                pp.Name = item;

                return pp;
            }

            if (item == equalBasicPayPeriodTemplate)
            {
                pp.Style = InsurancePeriodStyle.EqualBasicPayPeriod;
                pp.Value = 0;
                pp.Name = item;
            }

            throw new Exception(string.Format("{0}:无法匹配保险期限", item));
        }

        public static void CreateProperties(IProduct product, string[] items)
        {
            //product.InsurancePeriods.Clear();

            string[] s = items.GroupBy(p => p).Select(p2 => p2.Key).ToArray<string>();

            foreach (string v in s)
            {
                if (!string.IsNullOrEmpty(v))
                {
                    IInsurancePeriod pp = CreateProperty(v);
                    pp.Tag = true;
                    pp.Owner = product;
                    //product.InsurancePeriods.Add(pp);

                    IInsurancePeriod opp = product.InsurancePeriods.FirstOrDefault(p => p.Style == pp.Style && p.Value == pp.Value);

                    if (opp != null)
                    {
                        opp.Name = pp.Name;
                        opp.Tag = pp.Tag;
                    }
                    else
                    {
                        product.InsurancePeriods.Add(pp);
                    }
                }
            }

            foreach (IInsurancePeriod ip in product.InsurancePeriods)
            {
                if (ip.Tag == null)
                {
                    product.InsurancePeriods.Remove(ip);
                }
            }
                
        }
        

        public override bool IsToPropertiesEnable { get { return true; } }

        public override void ToProperties()
        {
            CreateProperties(Owner.Owner, Owner.Items.Split(';'));
            Owner.Owner.IsInsurancePeriodsFromFixRate = true;

        }
        
        public override bool  IsPolicySelectedEnable { get{return true;} }

        public override void ToPolicySelected(IPolicy policy, string item)
        {
            policy.SelectedInsurancePeriod = policy.Product.InsurancePeriods.First(p => p.Name == item);
        }

        public override string GetItemByPolicy(IPolicy policy)
        {
            string item = base.GetItemByPolicy(policy);

            if (string.IsNullOrEmpty(item) && policy.SelectedInsurancePeriod != null)
            {
                item = policy.SelectedInsurancePeriod.Name;
            }

            return item;
        }

    }
}
